package homework.level01model03.homework04;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentUpdate {

    public static void update(ArrayList<Student> arrayList){

        Scanner scanner = new Scanner(System.in);
        System.out.println("修改的学生姓名");
        String name = scanner.next();

        int index=-1;
        //查找此人
        for (Student student : arrayList) {
            if (name.equals(student.getName())){
                 index = arrayList.indexOf(student);
                //System.out.println(student);
                break;
            }
        }
        //判断此人有木有
        if (index==-1){
            System.out.println("查无此人");
        }else{
            Student student = arrayList.get(index);
            System.out.println(student);
            System.out.println("找到此人正在修改");
            System.out.println("输入要修改的姓名");
            String next = scanner.next();
            System.out.println("输入要修改的年龄");
            int i = scanner.nextInt();

            arrayList.get(index).setName(next);
            arrayList.get(index).setAge(i);
            System.out.println("修改成功");

        }

        //遍历集合全部元素
        for (Student student : arrayList) {
            System.out.println(student);
        }

    }
}
